﻿using HZY.Shared.Admin.Models.Accounts;
using System.Net;

namespace HZY.Api.Admin.Controllers.Indentity;

/// <summary>
/// 认证服务
/// </summary>
public class AccountController : IndentityControllerBase
{
    private readonly IConfiguration _configuration;
    private readonly IAccountService _accountService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="configuration"></param>
    /// <param name="accountService"></param>
    public AccountController(IConfiguration configuration, IAccountService accountService)
    {
        _configuration = configuration;
        _accountService = accountService;
    }

    /// <summary>
    /// 检查账户 登录信息 并返回 token
    /// </summary>
    /// <param name="authUserDto"></param>
    /// <returns></returns>
    [HttpPost]
    public Task<TokenInfo> LoginAsync([FromBody] AuthUserFormDto authUserDto)
    {
        return _accountService.LoginAsync(authUserDto);
    }

    /// <summary>
    /// Refresh Token
    /// </summary>
    /// <param name="refreshToken">refreshToken</param>
    /// <returns></returns>
    [HttpGet()]
    public async Task<TokenInfo> RefreshTokenAsync([FromQuery] string refreshToken)
    {
        var tokenInfo = await _accountService.RefreshTokenAsync(refreshToken);
        if (tokenInfo == null)
        {
            throw MessageBox.CreateMessage(R.ResultMessage(HttpStatusCode.Unauthorized, "未授权,请先授权访问!"));
        }

        return tokenInfo;
    }



    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet("{key}")]
    public object TestConfig([FromRoute] string key)
    {
        return new
        {
            conf = _configuration[key],
            key
        };
    }

}
